package com.javasm.adminapi.aftersales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.aftersales.service.B2cMagService;
import com.javasm.adminapi.module.aftersalesbase.dao.B2cReturnMsgDao;
import com.javasm.adminapi.module.aftersalesbase.entity.B2cReturnMsg;
import com.javasm.adminapi.module.aftersalesbase.entity.OmOrder;
import com.javasm.adminapi.module.aftersalesbase.entity.OmOrderGoods;
import com.javasm.adminapi.module.aftersalesbase.service.B2cReturnMsgService;
import com.javasm.adminapi.module.aftersalesbase.service.OmOrderGoodsService;
import com.javasm.adminapi.module.aftersalesbase.service.OmOrderService;
import com.javasm.adminapi.module.aftersalesbase.vo.FormVo;
import com.javasm.adminapi.module.aftersalesbase.vo.ReturnMagSearchVo;
import com.javasm.adminapi.module.product.entity.TSpu;
import com.javasm.adminapi.module.product.service.TSpuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @className: ReturnMagImpl
 * @description:
 * @author: zjl
 * @date: 2025/10/16 21:21
 * @version: 0.1
 * @since: jdk17
 */
@Service("b2CMagService")
public class B2cMagServiceImpl implements B2cMagService {
    @Resource
    B2cReturnMsgService b2cReturnMsgService;

    @Resource
    B2cReturnMsgDao b2cReturnMsgDao;

    @Resource
    OmOrderService omOrderService; // 需注入关联表的Service
    @Resource
    OmOrderGoodsService omOrderGoodsService;
    @Resource
    TSpuService tSpuService; // 假设存在TSpu的Service

    @Override
    public PageInfo<B2cReturnMsg> queryByCondition(ReturnMagSearchVo searchVo) {
        //b2cReturnMsgService.
        PageHelper.startPage(searchVo.getPageNum(),searchVo.getPageSize());
        //PageHelper.startPage(1, 10);
        LambdaQueryWrapper<B2cReturnMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(B2cReturnMsg::getMsgId);
        if (!StringUtils.isEmpty(searchVo.getNumber())) {
            queryWrapper.and(wrapper ->
                    wrapper.like(B2cReturnMsg::getMsgOrderNo, searchVo.getNumber()));
        }

        if (!StringUtils.isEmpty(searchVo.getUsername())) {
            queryWrapper.and(wrapper ->
                    wrapper.like(B2cReturnMsg::getMsgUsername, searchVo.getUsername()));
        }
        //审批人
        if(!StringUtils.isEmpty(searchVo.getAppName())){
            queryWrapper.and(wrapper ->
                    wrapper.like(B2cReturnMsg::getMsgAppName,searchVo.getAppName()));
        }
        if (searchVo.getStatus() != null) {
            queryWrapper.and(wrapper ->
                    wrapper.eq(B2cReturnMsg::getMsgStatus, searchVo.getStatus()));
        }
        if (!StringUtils.isEmpty(
                searchVo.getRecStart()) && !StringUtils.isEmpty(searchVo.getRecEnd())) {
            queryWrapper.and(i ->
                    i.apply("date_format(msg_create_time,'%Y-%m-%d') between {0} and {1}",
                            searchVo.getRecStart(), searchVo.getRecEnd())
            );

        }
        if (!StringUtils.isEmpty(
                searchVo.getAppStart()) && !StringUtils.isEmpty(searchVo.getAppEnd())) {
            queryWrapper.and(i ->
                    i.apply("date_format(msg_update_time,'%Y-%m-%d') between {0} and {1}",
                            searchVo.getAppStart(), searchVo.getAppEnd())
            );

        }

//        List<B2cReturnMsg> list = b2cReturnMsgDao.selectWithUserName(queryWrapper);
//        return new PageInfo<>(list);

        return new PageInfo<>(b2cReturnMsgService.list(queryWrapper));
    }




    @Override
    public void saveOrUpdate(B2cReturnMsg b2cReturnMsg) {

        // 2. 处理关联表OmOrder（若前端传递了omOrder数据）
        OmOrder omOrder = b2cReturnMsg.getOmOrder();
        TSpu tSpu = b2cReturnMsg.getTSpu();
        TSpu tsSpu = b2cReturnMsg.getTsSpu();
        if(b2cReturnMsg.getMsgId() == null){
            b2cReturnMsg.setMsgCreateTime(new Date());
            b2cReturnMsg.setMsgStatus(0);
            b2cReturnMsg.setMsgOrderCode(omOrder.getOoOrderCode());
            b2cReturnMsg.setMsgOriginalOrderId(tSpu.getSpuNo());
            b2cReturnMsg.setMsgTspuCode(tsSpu.getSpuNo());
            b2cReturnMsg.setMsgOrderNo("快递下单后生成...");
            b2cReturnMsg.setMsgAppName("待有缘人审核...");
        }

        b2cReturnMsg.setMsgUpdateTime(new Date());
        b2cReturnMsg.setMsgUsername(omOrder.getOoCneeName());
        b2cReturnMsgService.saveOrUpdate(b2cReturnMsg);

    }

    @Override
    public List<B2cReturnMsg> listByCondition(ReturnMagSearchVo searchVo) {
        LambdaQueryWrapper<B2cReturnMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(B2cReturnMsg::getMsgId);
        if (!StringUtils.isEmpty(searchVo.getNumber())) {
            queryWrapper.and(wrapper ->
                    wrapper.eq(B2cReturnMsg::getMsgOrderNo, searchVo.getNumber()));
        }

        if (!StringUtils.isEmpty(searchVo.getUsername())) {
            queryWrapper.and(wrapper ->
                    wrapper.like(B2cReturnMsg::getMsgUsername, searchVo.getUsername()));
        }
        //审批人
        if(!StringUtils.isEmpty(searchVo.getAppName())){
            queryWrapper.and(wrapper ->
                    wrapper.like(B2cReturnMsg::getMsgAppName,searchVo.getAppName()));
        }
        if (searchVo.getStatus() != null) {
            queryWrapper.and(wrapper ->
                    wrapper.eq(B2cReturnMsg::getMsgStatus, searchVo.getStatus()));
        }
        if (!StringUtils.isEmpty(
                searchVo.getRecStart()) && !StringUtils.isEmpty(searchVo.getRecEnd())) {
            queryWrapper.and(i ->
                    i.apply("date_format(create_time,'%Y-%m-%d') between {0} and {1}",
                            searchVo.getRecStart(), searchVo.getRecEnd())
            );

        }
        if (!StringUtils.isEmpty(
                searchVo.getAppStart()) && !StringUtils.isEmpty(searchVo.getAppEnd())) {
            queryWrapper.and(i ->
                    i.apply("date_format(update_time,'%Y-%m-%d') between {0} and {1}",
                            searchVo.getAppStart(), searchVo.getAppEnd())
            );

        }
        return b2cReturnMsgService.list(queryWrapper);

    }



    @Override
    public List<OmOrder> listOrderByCondition(FormVo searchVo) {
        LambdaQueryWrapper<OmOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(OmOrder::getOoOrderCode,searchVo.getOoOrderCode());
        return omOrderService.list(queryWrapper);
    }

    @Override
    public List<TSpu> listTspuByCondition(FormVo searchVo) {
        LambdaQueryWrapper<TSpu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(TSpu::getSpuNo,searchVo.getGoodsCode());
        queryWrapper.like(TSpu::getSpuName,searchVo.getGoodsName());
        return tSpuService.list(queryWrapper);
    }

    @Override
    public void removeByIds(Integer[] ids) {
        b2cReturnMsgService.removeByIds(List.of(ids));
    }

    @Override
    public B2cReturnMsg getById(Integer id) {
        return b2cReturnMsgDao.selectWithUserName(id);
    }




}